//
//  TimeManager.swift
//  solarbrain
//
//  Created by MoennigQ on 8/11/18.
//  Copyright © 2018 Client.Solution Pty Ltd. All rights reserved.
//

import Foundation

class SBTimeManager: NSObject {
    static let shared: SBTimeManager = SBTimeManager()
    let SBTimeStamp: String = "SBTimeStamp"
    let SBTimeInternal: TimeInterval = 7 * 24 * 60 * 60
    
    func resetTime(){
        let currentTime: NSNumber = NSNumber(value: NSDate().timeIntervalSince1970)
        UserDefaults.standard.set(currentTime, forKey: SBTimeStamp)
    }
    
    func isOver() -> Bool {//token 7天有效期
        let currentTime: TimeInterval = NSDate().timeIntervalSince1970
        var oldTimeStamp = UserDefaults.standard.object(forKey: SBTimeStamp) as? NSNumber
        if oldTimeStamp == nil {
            self.resetTime()
            oldTimeStamp = UserDefaults.standard.object(forKey: SBTimeStamp) as? NSNumber
        }
        
        let oldTime: TimeInterval = oldTimeStamp!.doubleValue
        if (currentTime - oldTime) > SBTimeInternal {
            return true
        }else{
            return false
        }
    }
}
